package com.android.lovingpet.mine.pet.view;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import com.android.lovingpet.R;
import com.android.lovingpet.base.TakePhotoFragment;
import com.android.lovingpet.bean.Pet;
import com.android.lovingpet.mine.pet.IPetContract;
import com.android.lovingpet.mine.pet.PetPresenter;
import com.android.lovingpet.util.MyDatePickDialog;
import com.android.lovingpet.util.MyPhotoWindow;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.codbking.widget.bean.DateType;
import com.jph.takephoto.model.TResult;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by ikata on 2018/4/29.
 */

public class AddPetFragment extends TakePhotoFragment<IPetContract.View, PetPresenter> implements IPetContract.View, View.OnClickListener, AdapterView.OnItemSelectedListener {
    @BindView(R.id.iv_man)
    ImageView ivMan;
    @BindView(R.id.iv_woman)
    ImageView ivWoman;
    @BindView(R.id.bt_ok)
    Button btOk;
    Unbinder unbinder;
    @BindView(R.id.civ_head)
    CircleImageView civHead;
    @BindView(R.id.sp_type)
    Spinner spType;
    @BindView(R.id.sp_sterilize)
    Spinner spSterilize;
    @BindView(R.id.tv_birthday)
    TextView tvBirthday;
    @BindView(R.id.et_name)
    EditText etName;

    private int sex=1;

    private File headFile;

    @Override
    protected int setContentView() {
        return R.layout.fragment_add_pet;
    }

    @Override
    protected void onCreateView(View view) {
        unbinder = ButterKnife.bind(this, view);
        setTitle(view,"添加宠物");
        initView();
    }

    public void initView() {
        spType.setOnItemSelectedListener(this);
        spSterilize.setOnItemSelectedListener(this);
        civHead.setOnClickListener(this);
        setGender(1);
    }


    @Override
    public PetPresenter createPresenter() {
        return new PetPresenter();
    }

    @Override
    public IPetContract.View createView() {
        return this;
    }

    @Override
    public void next() {
        getFragmentManager().popBackStack();
    }

    @Override
    public void showMyPet(Pet pet) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.bt_ok, R.id.civ_head, R.id.tv_birthday,R.id.iv_man,R.id.iv_woman})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_ok:
                String name=etName.getText().toString();
                String birthday=tvBirthday.getText().toString();
                String type=spType.getSelectedItem().toString();
                int sterilize=spSterilize.getSelectedItemPosition();
                if (headFile!=null&&!TextUtils.isEmpty(name)&&!TextUtils.isEmpty(type)&&!TextUtils.isEmpty(tvBirthday.getText().toString())) {
                    getPresenter().addPet(name, type, birthday,sterilize, sex, headFile);
                }
                break;
            case R.id.civ_head:
                new MyPhotoWindow(takePhoto, getView()).create();
                break;
            case R.id.tv_birthday:
                @SuppressLint("SimpleDateFormat") DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                MyDatePickDialog.Builder builder = new MyDatePickDialog.Builder(getContext());
                builder.setTitle("选择宠物的生日")
                        .setMessageFormat("yyyy-MM-dd")
                        .setYearLimit(15)
                        .setType(DateType.TYPE_YMD)
                        .setOnSureLisener(date -> {
                            tvBirthday.setText(dateFormat.format(date));
                        }).build().show();
                break;
            case R.id.iv_man:
                setGender(1);
                break;
            case R.id.iv_woman:
                setGender(0);
                break;
        }
    }

    @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);
        headFile = new File(result.getImage().getOriginalPath());
        Glide.with(this)
                .load(headFile)
                .skipMemoryCache(true)
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .into(civHead);

    }

    @Override
    public void takeFail(TResult result, String msg) {
        super.takeFail(result, msg);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    }

    private void setGender(int sex) {
        this.sex = sex;
        if (sex == 1) {
            ivMan.setImageDrawable(getContext().getResources().getDrawable(R.drawable.ic_man));
            ivWoman.setImageDrawable(getContext().getResources().getDrawable(R.drawable.ic_woman1));
        } else {
            ivMan.setImageDrawable(getContext().getResources().getDrawable(R.drawable.ic_man1));
            ivWoman.setImageDrawable(getContext().getResources().getDrawable(R.drawable.ic_woman));
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

}
